package com.zhongxi.guoyuan.adapter.personCenter;

import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.entity.clearweed.ClearWeedGood;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.CommonTool;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/1/6.
 */

public class ExchangeCateItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private List<ClearWeedGood> list;

    private int footer_state = 0;
    private OnItemClickListener itemClickListener;

    //普通布局
    static final int TYPE_ITEM = 0;
    //底部布局
    static final int TYPE_FOOTER = 1;

    //正在加载更多
    public static final int LOAD_MORE = 1;

    //没有更多
    public static final int NO_MORE = 2;

    //初始状态
    public static final int INIT = 0;

    public ExchangeCateItemAdapter(List<ClearWeedGood> list) {
        this.list = list;
    }

    public void setFooter_state(int footer_state) {
        this.footer_state = footer_state;
        notifyDataSetChanged();
    }

    public int getFooter_state() {
        return footer_state;
    }

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view = View.inflate(parent.getContext(), R.layout.cate_exchange_layout, null);
            RecycleViewHolder viewHolder = new RecycleViewHolder(view);
            return viewHolder;
        } else if (viewType == TYPE_FOOTER) {
            View view = View.inflate(parent.getContext(), R.layout.recycle_footer_layout, null);
            FooterViewHolder footerViewHolder = new FooterViewHolder(view);
            return footerViewHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof RecycleViewHolder) {
            ClearWeedGood clearWeedGood = list.get(position);

            if (null != clearWeedGood && !"".equals(clearWeedGood)) {
                Glide.with(((RecycleViewHolder) holder).exchangeItemImage.getContext())
                        .load(Url.IMG_URL + clearWeedGood.getThumbnail())
                        .asBitmap()
                        .placeholder(R.mipmap.hicc)
                        .centerCrop()
                        .into(((RecycleViewHolder) holder).exchangeItemImage);


                if (null != clearWeedGood.getName() && !"".equals(clearWeedGood.getName())) {
                    ((RecycleViewHolder) holder).exchangeItemName.setText(clearWeedGood.getName() + "");
                } else {
                    ((RecycleViewHolder) holder).exchangeItemName.setText("");
                }
                if (null != clearWeedGood.getMarketprice() && !"".equals(clearWeedGood.getMarketprice())) {
                    ((RecycleViewHolder) holder).exchangeItemOriginalPrice.setText("原价:" + clearWeedGood.getMarketprice() + "元");
                    ((RecycleViewHolder) holder).exchangeItemOriginalPrice.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                } else {
                    ((RecycleViewHolder) holder).exchangeItemOriginalPrice.setText("");
                }
                if (null != clearWeedGood.getOrdernum() && !"".equals(clearWeedGood.getOrdernum())) {
                    ((RecycleViewHolder) holder).exchangeItemHasExchange.setText(clearWeedGood.getOrdernum() + "人已兑");
                } else {
                    ((RecycleViewHolder) holder).exchangeItemHasExchange.setText("0人已兑");
                }
                if (null != clearWeedGood.getInnerprice() && !"".equals(clearWeedGood.getInnerprice())) {
                    ((RecycleViewHolder) holder).exchangeItemClearWeed.setText(clearWeedGood.getInnerprice() + "美豆");

                } else {
                    ((RecycleViewHolder) holder).exchangeItemClearWeed.setText("");


                }
                if (null != clearWeedGood.getBackcashnum() && !"".equals(clearWeedGood.getBackcashnum())) {
                    ((RecycleViewHolder) holder).exchangeReward.setText("兑成奖：" + clearWeedGood.getBackcashnum() + "元");
                } else {
                    ((RecycleViewHolder) holder).exchangeReward.setText("");
                }
                if (null != clearWeedGood.getRedbeanprice() && !"".equals(clearWeedGood.getRedbeanprice())) {
                    ((RecycleViewHolder) holder).getExchangeItemRedBean.setText("+" + clearWeedGood.getRedbeanprice() + "红豆");
                } else {
                    ((RecycleViewHolder) holder).getExchangeItemRedBean.setText("");
                }

                if (!TextUtils.isEmpty(clearWeedGood.getLongitude()) && !TextUtils.isEmpty(clearWeedGood.getLatitude())
                        && !TextUtils.isEmpty(MyApplication.getInstance().getLongitude()) && !TextUtils.isEmpty(MyApplication.getInstance().getLatitude())) {
                    ((RecycleViewHolder) holder).exchangeItemDistance.setText("距您:" + CommonTool.getDistance(Double.parseDouble(clearWeedGood.getLongitude()),
                            Double.parseDouble(clearWeedGood.getLongitude()),
                            Double.parseDouble(MyApplication.getInstance().getLongitude()),
                            Double.parseDouble(MyApplication.getInstance().getLatitude())));
                } else {
                    ((RecycleViewHolder) holder).exchangeItemDistance.setText("定位失败");
                }

                if (TextUtils.isEmpty(clearWeedGood.getAddress())) {
                    ((RecycleViewHolder) holder).exchangeItemAddress.setText("商家:" + clearWeedGood.getAddress());
                } else {
                    ((RecycleViewHolder) holder).exchangeItemAddress.setText("");
                }

                if (clearWeedGood.getAmountofmail() != 0) {
                    ((RecycleViewHolder) holder).exchangePosetFree.setVisibility(View.VISIBLE);
                } else {
                    ((RecycleViewHolder) holder).exchangePosetFree.setVisibility(View.INVISIBLE);
                }

            }
        } else if (holder instanceof FooterViewHolder) {

            switch (footer_state) {
                case LOAD_MORE://
                    ((FooterViewHolder) holder).recycleProgressbar.setVisibility(View.VISIBLE);
                    ((FooterViewHolder) holder).recycleTvLine1.setVisibility(View.GONE);
                    ((FooterViewHolder) holder).recycleTvLine2.setVisibility(View.GONE);
                    ((FooterViewHolder) holder).recycleFootTv.setVisibility(View.VISIBLE);

                    ((FooterViewHolder) holder).recycleFootTv.setText("正在加载...");
                    break;
                case NO_MORE://
                    ((FooterViewHolder) holder).recycleProgressbar.setVisibility(View.GONE);
                    ((FooterViewHolder) holder).recycleTvLine1.setVisibility(View.GONE);
                    ((FooterViewHolder) holder).recycleTvLine2.setVisibility(View.GONE);
                    ((FooterViewHolder) holder).recycleFootTv.setVisibility(View.VISIBLE);

                    ((FooterViewHolder) holder).recycleFootTv.setText("已经到最底部了");
                    break;
                case INIT://上拉加载
                    ((FooterViewHolder) holder).recycleProgressbar.setVisibility(View.GONE);
                    ((FooterViewHolder) holder).recycleFootTv.setVisibility(View.GONE);
                    ((FooterViewHolder) holder).recycleTvLine1.setVisibility(View.GONE);
                    ((FooterViewHolder) holder).recycleTvLine2.setVisibility(View.GONE);
                    break;
            }
        }

    }


    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public int getItemCount() {
        return list != null ? list.size() + 1 : 0;
    }

    class RecycleViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        @Bind(R.id.exchange_item_image)
        ImageView exchangeItemImage;
        @Bind(R.id.exchange_item_name)
        TextView exchangeItemName;
        @Bind(R.id.exchange_item_originalPrice)
        TextView exchangeItemOriginalPrice;
        @Bind(R.id.exchange_item_hasExchange)
        TextView exchangeItemHasExchange;
        @Bind(R.id.exchange_item_clearWeed)
        TextView exchangeItemClearWeed;
        @Bind(R.id.exchange_item_distance)
        TextView exchangeItemDistance;
        @Bind(R.id.exchange_item_address)
        TextView exchangeItemAddress;
        @Bind(R.id.exchange_item_redbean)
        TextView getExchangeItemRedBean;
        @Bind(R.id.exchange_postage_free)
        TextView exchangePosetFree;
        @Bind(R.id.excahnge_item_reward)
        TextView exchangeReward;

        public RecycleViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {

            if (itemClickListener != null) {
                itemClickListener.onClick(view, getAdapterPosition());

            }
        }
    }

    class FooterViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.recycle_tv_line1)
        TextView recycleTvLine1;
        @Bind(R.id.recycle_progressbar)
        ProgressBar recycleProgressbar;
        @Bind(R.id.recycle_foot_tv)
        TextView recycleFootTv;
        @Bind(R.id.recycle_tv_line2)
        TextView recycleTvLine2;
        @Bind(R.id.recycle_container_relative)
        RelativeLayout recycleContainerRelative;

        public FooterViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public static interface OnItemClickListener {
        void onClick(View view, int position);
    }

}
